package LeetCode;

/**
 * @author VX5
 * @Title: MJC
 * @ProjectName DataStructure
 * @Description: TODO
 * @date ${DAT}18:06
 */
public class E9 {
    public static class ListNode{
        int val;
        ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public static void main(String[] args) {
        ListNode head = new ListNode(4);
        head.next = new ListNode(5);
        head.next.next = new ListNode(1);
        head.next.next.next = new ListNode(9);
        ListNode node = new E9().deleteNode(head,4);
        if (node != null) {
            while (node != null) {
                System.out.printf(node.val + "\t");
                node = node.next;
            }
        }else {
            System.out.printf("没有此数");
        }
    }

    public ListNode deleteNode(ListNode head, int val) {
        if (head == null){
            return null;
        }
        ListNode node = head;
        if (node.val == val){
            return head.next;
        }
        while (true){
            if (node.next.val == val){
                node.next = node.next.next;
                break;
            }
            node = node.next;
        }
        return head;
    }

}
